package net.yla.smartbus.core.util;


import net.yla.smartbus.core.util.enums.SubstringType;

/**
 * @Author : 默苍离
 * @Create_Date : 2020/08/11/10:43
 * @Pack_name : org.jgs1904 : MySubstring
 * *****************************************************
 * 注释: 字符串截断方法
 */
public class Substring {

    public static String substring(String s, int count) {

        if (count <= 0) {
            s = "";
        } else if (count > s.length()) {
            s = s;
        } else {
            s = s.substring(0, count);
        }

        return s;

    }

    public static String subString(String s, int count, String overflow) {

        int o = overflow.length();

        if (count <= 0) {
            s = "";
        } else if (count >= s.length() + o) {
            s = s + overflow;
        } else if (count > s.length() && count < s.length() + o) {
            s = (s + overflow).substring(0, count) + "...";
        } else {
            s = substring(s, count) + "...";
        }

        return s;

    }

    public static String subString(String s, int count, String overflow, SubstringType type) {

        int o = overflow.length();

        if (count <= 0) {

            s = "";

        } else if (type == SubstringType.LEFT) {

            if (count >= s.length() + o) {
                s = s + overflow;
            } else if (count > s.length() && count < s.length() + o) {
                s = (s + overflow).substring(0, count) + "...";
            } else {
                s = substring(s, count) + "...";
            }

        } else if (type == SubstringType.RIGHT) {

            if (count >= s.length() + o) {
                s = s + overflow;
            } else if (count > s.length() && count <= s.length() + o) {
                s = s + overflow;
                s = "..." + s.substring(s.length() - count);
            } else {
                s = s + overflow;
                s = "..." + s.substring(s.length() - count);
            }

        } else if (type == SubstringType.MIDDLE) {

            if (count >= (s.length() + o)) {
                s = "..." + (s + overflow).substring((s.length() + o) / 2);
            } else if (count >= s.length() && count < (s.length() + o)) {
                s = "..." + (s + overflow).substring((s.length() + o) / 2, (s.length() + o) / 2 + count) + "...";
            } else {
                s = "..." + (s + overflow).substring((s.length() + o) / 2, (s.length() + o) / 2 + count) + "...";
            }

        } else if (type == SubstringType.LEFTRIGHT) {

            if (count >= (s.length() + o)) {
                s = s + overflow;
            } else if ((s.length() + o) / 2 - count <= 0) {
                s = "";
            } else {
                s = "..." + (s + overflow).substring(count, s.length() + o - count) + "...";
            }

        }

        return s;

    }

}
